package com.quick.controller.system;

import java.util.List;

import com.quick.system.domain.SysUser;
import com.quick.system.domain.SysUserRole;
import com.quick.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.quick.common.annotation.Log;
import com.quick.common.core.controller.BaseController;
import com.quick.common.core.domain.ApiResponse;
import com.quick.common.enums.BusinessTypeEnum;
import com.quick.system.domain.SysRole;
import com.quick.system.service.SysRoleService;
import com.quick.common.utils.poi.ExcelUtil;

/**
 * 角色信息Controller
 *
 * @Author: 吃素不吃肉
 * @Date: Tue Jul 25 14:14:44 CST 2023
 */
@Api(value = "角色信息管理", tags = {"角色信息操作接口"})
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserService sysUserService;
    /**
     * 查询角色信息列表
     */
    @PreAuthorize("@ps.hasPermission('system:role:list')")
    @GetMapping("/list")
    @ApiOperation(value = "分页查询角色信息列表")
    public ApiResponse list(SysRole sysRole) {
        try {
            startPage();
            List<SysRole> list = sysRoleService.selectSysRoleList(sysRole);
            return paging(list);
        } finally {
            clearPage();
        }

    }

    /**
     * 导出角色信息列表
     */
    @PreAuthorize("@ps.hasPermission('system:role:export')")
    @Log(title = "角色信息", businessType = BusinessTypeEnum.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出角色信息列表")
    public void export(HttpServletResponse response, SysRole sysRole) {
        List<SysRole> list = sysRoleService.selectSysRoleList(sysRole);
        ExcelUtil<SysRole> util = new ExcelUtil<>(SysRole.class);
        util.exportExcel(response, list, "角色信息数据");
    }

    /**
     * 获取角色信息详细信息
     */
    @PreAuthorize("@ps.hasPermission('system:role:query')")
    @GetMapping(value = "/{roleId}")
    @ApiOperation(value = "获取角色信息详细信息")
    public ApiResponse getInfo(@PathVariable("roleId") Long roleId) {
        return success(sysRoleService.selectSysRoleByRoleId(roleId));
    }

    /**
     * 新增角色信息
     */
    @PreAuthorize("@ps.hasPermission('system:role:add')")
    @Log(title = "角色信息", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    @ApiOperation(value = "新增角色信息")
    public ApiResponse add(@RequestBody SysRole sysRole) {
        return success(sysRoleService.insertSysRole(sysRole));
    }

    /**
     * 修改角色信息
     */
    @PreAuthorize("@ps.hasPermission('system:role:edit')")
    @Log(title = "角色信息", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改角色信息")
    public ApiResponse edit(@RequestBody SysRole sysRole) {
        return success(sysRoleService.updateSysRole(sysRole));
    }

    /**
     * 删除角色信息
     */
    @PreAuthorize("@ps.hasPermission('system:role:remove')")
    @Log(title = "角色信息", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{roleIds}")
    @ApiOperation(value = "删除角色信息")
    public ApiResponse remove(@PathVariable Long[] roleIds) {
        return success(sysRoleService.deleteSysRoleByRoleIds(roleIds));
    }

    /**
     * 修改保存数据权限
     */
    @PreAuthorize("@ps.hasPermission('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/dataScope")
    public ApiResponse dataScope(@RequestBody SysRole role) {
        return success(sysRoleService.authDataScope(role));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ps.hasPermission('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/changeStatus")
    public ApiResponse changeStatus(@RequestBody SysRole role) {
        return success(sysRoleService.updateRoleStatus(role));
    }


    /**
     * 查询已分配用户角色列表
     */
    @PreAuthorize("@ps.hasPermission('system:role:list')")
    @GetMapping("/authUser/allocatedList")
    public ApiResponse allocatedList(SysUser user) {
        try {
            startPage();
            List<SysUser> list = sysUserService.selectAllocatedList(user);
            return paging(list);
        } finally {
            clearPage();
        }
    }

    /**
     * 查询未分配用户角色列表
     */
    @PreAuthorize("@ps.hasPermission('system:role:list')")
    @GetMapping("/authUser/unAllocatedList")
    public ApiResponse unAllocatedList(SysUser user) {
        try {
            startPage();
            List<SysUser> list = sysUserService.selectUnAllocatedList(user);
            return paging(list);
        } finally {
            clearPage();
        }
    }

    /**
     * 取消授权用户
     */
    @PreAuthorize("@ps.hasPermission('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessTypeEnum.GRANT)
    @PutMapping("/authUser/cancel")
    public ApiResponse cancelAuthUser(Long roleId, Long userId) {
        return success(sysRoleService.deleteAuthUser( roleId,  userId));
    }

    /**
     * 批量取消授权用户
     */
    @PreAuthorize("@ps.hasPermission('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessTypeEnum.GRANT)
    @PutMapping("/authUser/cancelAll")
    public ApiResponse cancelAuthUserAll(Long roleId, Long[] userIds) {
        return success(sysRoleService.deleteAuthUsers(roleId, userIds));
    }

    /**
     * 批量选择用户授权
     */
    @PreAuthorize("@ps.hasPermission('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessTypeEnum.GRANT)
    @PutMapping("/authUser/selectAll")
    public ApiResponse selectAuthUserAll(Long roleId, Long[] userIds) {
        return success(sysRoleService.insertAuthUsers(roleId, userIds));
    }
}
